<!--
 * @Author: Nxf
 * @Date: 2024-06-13 12:03:34
 * @LastEditors: Nxf
 * @LastEditTime: 2024-07-29 20:32:25
 * @Descripttion: 
-->

<template>
    <a-spin :spinning="spinning" tip="加载中...">

        <div id="printDiv">
            <a-button type="primary" v-print="printContent" class="hiddenDiv">
                打印送货清单
                <!-- ({{ shop_type }}) -->
            </a-button>
            <template v-if="shop_type == 'shop'">
                <template v-for="item in saleOrders" :key="item.id">
                    <div style="page-break-after:always">
                        <ShopTables :saleOrder="item" />
                    </div>
                </template>
            </template>
            <template v-else-if="shop_type == 'supermarket'">
                <template v-for="item in saleOrders" :key="item.id">
                    <div style="page-break-after:always">
                        <ShopTables :saleOrder="item" />
                    </div>
                </template>
            </template>
            <template v-else-if="shop_type == 'school'">
                <template v-for="item in saleOrders" :key="item.id">
                    <div style="page-break-after:always">
                        <ShopTables :saleOrder="item" />
                    </div>
                </template>
            </template>
        </div>
    </a-spin>

</template>
<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { get_api } from '@/odoojs/index.js'
import router from '../router/index'
import ShopTables from '../components/shopTables.vue'
//
const printContent = ref({
    id: 'printDiv',
    popTitle: "",//页眉标题
    preview: false,  // 是否开启预览
    beforeOpenCallback: () => {
        // console.log('------ 开始打印之前的callback ------')
    }
})
//
const rpc = get_api()

const session_info = ref();
const so_ids = ref();
const shop_type = ref();
//
var saleOrders = ref([]);
const spinning = ref(false);

onMounted(async () => {
    session_info.value = await rpc.session_check()
    console.log('----session_info---', session_info.value);

    const currentRoute = router.currentRoute.value
    const query = currentRoute.query

    const ids_list = Array.isArray(query.id) ? query.id : [query.id]
    const ids = ids_list.map(item => Number(item))
    so_ids.value = ids
    shop_type.value = query.type || 'shop'
    console.log('-=-=-', query, so_ids.value, shop_type.value)

    // const domain = [('id', 'in', so_ids.value)]
    //
    await getOrderData()
})
async function getOrderData() {
    spinning.value = true;
    const model = 'sale.order'
    const Model = rpc.env.model(model)
    // const domain = []  // 过滤条件. 选择今天的 调度单
    const fields = {
        company_id: {
            fields: {
                name: {},
                display_name: {},
                mobile: {}
            }

        },//供货人
        dest_station_id: {
            fields: {
                display_name: {},
                partner_id: {
                    fields: {
                        display_name: {},
                        mobile: {},
                        street2: {}
                    }
                }
            }
        },//收货人
        partner_id: {
            fields: {
                display_name: {},
                mobile: {},
                street2: {}
            }
        },//客户
        client_order_ref: {},
        user_id: {},// 制表人，审核员
        date_order: {},//
        amount_total: {},//
        order_line: {
            fields: {
                product_id: {
                    fields: {
                        barcode: {},
                        name: {},
                    },
                },
                product_uom: {},
                product_uom_qty: {},
                price_unit: {},
                price_total: {},
            }
        }
    }
    await Model.load_metadata({ fields })
    const domain = [['id', 'in', so_ids.value]]

    const res = await Model.load_data({ domain, fields });
    // const res = await Model.web2_read(so_ids.value, { fields });

    console.log('-=-=order info--=-=-', res);
    saleOrders.value = res;
    console.log('-=-=saleOrders--=-=-', saleOrders);

    if (res.length) {
        // res.map((item) => {
        //     const goodsInfo = {
        //         'goodsId': '',//货物ID
        //         'barCode': '',//条码
        //         'goodsName': '',//货物名称
        //         'goodsNorms': '',//货物规格
        //         'goodsUnit': '',//货物单位
        //         'goodsQuantity': '',//采购数量
        //         'buyingPrice': '',//供货价
        //         'piecesNum': '',//件数
        //         'totalPrice': ''//总价
        //     }
        //     goodsInfo.goodsId = item.order_line[0].product_id.id || '';
        //     goodsInfo.barCode = item.order_line[0].product_id.barcode || '';
        //     goodsInfo.goodsName = item.order_line[0].product_id.name || '';
        //     goodsInfo.goodsNorms = '';
        //     goodsInfo.goodsUnit = item.order_line[0].product_uom.display_name || '';
        //     goodsInfo.goodsQuantity = item.order_line[0].product_uom_qty || '';
        //     goodsInfo.buyingPrice = item.order_line[0].price_unit || '';
        //     goodsInfo.piecesNum = '';
        //     goodsInfo.totalPrice = item.order_line[0].price_total || '';

        // })
        spinning.value = false;
    } else {
    }
}

</script>
<style lang="less" type="text/less" scoped>
#printDiv {
    width: 100vw;
    height: 100vh;
    padding: 30px;
    font-family: Microsoft YaHei !important;
    color: #000 !important;
    // background-color: yellow;
}

.hiddenDiv {
    float: right;
    margin-bottom: 50px;
}

@media print {
    .hiddenDiv {
        display: none;
    }

    // #printDiv {
    //     font-family: Microsoft YaHei !important;
    //     // color: #000 !important;
    //     color: red !important;
    //     background-color: yellow;
    // }
}
</style>
